# -*- coding: utf-8 -*-
'''
urllib的封装
'''
from __future__ import print_function
import urllib.request, urllib.error
import gzip
import socket

# 获取网页数据
def get_content_utf8(url):
    return get_content(url, 'utf-8')

# 获取网页数据
def get_content(url, encoding):
    # 10秒内没有打开web页面，就算超时

    fp = urllib.request.urlopen(url, timeout=10)
    html = fp   .read()
    try:
        details = gzip.decompress(html).decode('utf-8')
    except:
        details = html.decode(encoding)
    finally:
        fp.close()

    return details

def get_content2_utf8(url):
    return get_content2(url, 'utf-8')

# 获取网页数据, using urllib2
def get_content2(url, encoding):
    timeout = 10
    socket.setdefaulttimeout(timeout)
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36'}
    urlreq = urllib.request.URLopener()
    urlreq.addheader('User-Agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36')
    try:
        fp = urlreq.open(url);
        html = fp.read()
    except:
        print("Error when load ", url)
        return None

    try:
        details = gzip.decompress(html).decode('utf-8')
    except:
        details = html.decode(encoding)
    finally:
        fp.close()

    return details


if __name__== '__main__':
    # headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36'}
    url = "http://d.10jqka.com.cn/v2/realhead/hs_002438/last.js"
    # req = urllib.Request(url=url, headers=headers)
    # html = urllib.urlopen(req, timeout=10).read()
    # print("result=", html)
    #print(get_content_utf8(url))
    print(get_content2_utf8(url))